package com.itheima.filter;


import com.itheima.common.BaseContext;
import com.itheima.common.EmployeeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author zhangwenjie
 * @Desc: 登录过滤器
 * @Date 14:59 2022/6/5
 * @Version 1.0
 **/
@WebFilter
@Slf4j
public class LoginCheckFilter implements Filter {
    /**
     * 支持通配符的路径匹配对象
     */

    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    /**
     * 请求放行的路径
     */
    private static final String[] passUrls = {
            "/employee/login",
            "/backend/page/login/login.html",
            "/backend/api/**",
            "/backend/images/**",
            "/backend/plugins/**",
            "/backend/styles/**",
            "/backend/js/**"
    };

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取本次访问路径
        String requestURI = request.getRequestURI();
        //调用下面的方法进行对比,查看本次路径是否再释放路径中
        boolean check = check(requestURI);
        //如果等于true就放行
        if (check){
            filterChain.doFilter(request,response);
            return;
        }
        //再cookis中查询
        String empId = null;
        Cookie[] cookies = request.getCookies();
        //查找有没有有没有员工登录
        for (Cookie cookie : cookies) {
            if (EmployeeConstant.TOKEN.equals(cookie.getName())){
                empId=cookie.getValue();
            }
        }
        //如果没有就说明没有登录,返回登陆页面
        if (empId==null){
            response.sendRedirect("/backend/page/login/login.html");
            return;
        }
        //根据cookis的Value查找对应的session
        Object empID  = request.getSession().getAttribute(empId);
        //判断,如果找不到,说明没有登录,返回登录页面
        if (empID ==null){
            response.sendRedirect("/backend/page/login/login.html");
            return;
        }
        Long eId = (Long) empID;
        BaseContext.setCurrentId(eId);
        //如果走到这里,说明已经找到cookis和session,那么就可以放行
        log.info("用户已登录，{}，请求放行" + request);
        filterChain.doFilter(request,response);
        return;
    }
    /**
     * 路径匹配，检查本次请求是否需要放行
     *
     * @param requestURI
     * @return true-放行,false-拦截
     */
    public boolean check(String requestURI){
        boolean flag = false;
        for (String passUrl : passUrls) {
            //判断请求路径是否包含，放行路径
            if (PATH_MATCHER.match(passUrl,requestURI)){
                flag=true;
                break;
            }
        }
        return flag;
    }
}
